package com.wxapp.common.constant.base;

import com.wxapp.common.constant.pojo.vm.ResponseVm;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

/**
 * @author lgl
 */
public class IBaseController extends ResponseVm<Object> {

    public HttpServletResponse getResponse() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes == null ? null : servletRequestAttributes.getResponse();
    }

    public HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes == null ? null : servletRequestAttributes.getRequest();
    }

    protected void exportFile(byte[] files) {
        exportFile(getResponse(),files, null);
    }
    protected void exportFile(byte[] files,String name) {
        exportFile(getResponse(),files, name);
    }
}
